<div class="container-fluid">
    <h4>管理人员信息</h4>
    <div class="table-wrapper users-table section">
        <div class="row-fluid head">
        </div>

        <div class="input-group" id="search">
            &nbsp;<label for="username">登录名：</label>
            <input type="text" name="username" class="input-small" id="username">
            <label for="realname">&nbsp;姓名：</label>
            <input type="text" name="realname" class="input-small" id="realname">
       <span>
           <button id="search_btn" type="button" class="btn btn-purple btn-sm">
               搜索
               <i class="icon-search icon-on-right bigger-110"/>
           </button>
       </span>
        </div>
        <div id="list"></div>
    </div>
</div>

<script>
    (function () {
        var url = 'secUser/list-data.do';
        var $datagrid = $('#list');

        loadData();

        $('#search_btn').on('click', function () {
            loadData();
        });

        function loadData() {
            $datagrid.datagrid({
                action: true,
                doUpdate: function (rowData) {
                    doUpateData(rowData.id);
                },
                doDel: function (rowData) {
                    doDelete(rowData.id);
                },
                toolbar: [
                    {
                        type: 'ADD',
                        click: function () {
                            addSecUser();
                        }
                    }
                ],
                'dataModel': {
                    'url': url,
                    'parameters': getCommJosnParams('search')
                },
                'columns': [
                    {title: '登录名', dataField: 'username'},
                    {title: '真实姓名', dataField: 'realname'},
                    {title: '状态', dataField: 'statusDict'},
                    {title: '创建时间', dataField: 'createTime'}
                ]
            });
        };

        function doDelete(id) {
            layer.confirm("确定删除此用户", function () {
                $.post('secUser/delete.do', {"id": id}, function (result) {
                    if (result.success) {
                        loadData();
                        layer.alert("删除成功", {icon: 1});
                        layer.closeAll();
                    } else {
                        layer.alert("删除失败", {icon: 5});
                    }
                }, "json");
            })
        }

        function addSecUser() {
            var $div = $('<div>');
            $div.load('secUser/form.do', function (response, status, xhr) {
                if (xhr.status == 200) {
                    layer.open({
                        type: 1,//Page层类型
                        area: ['300px', '260px'],
                        title: '增加管理员',
                        btn: ['提交'],
                        content: $div.html(),
                        yes: function (layId) {
                            var param = $('#secUserForm').serialize();
                            $.post('secUser/add.do', param, function (result) {
                                if (result.success) {
                                    loadData();
                                    layer.msg('新增成功', {icon: 1});
                                    layer.close(layId)
                                } else {
                                    layer.msg(result.message, {icon: 5});
                                }
                            }, 'json')
                        }
                    });
                }
            });
        }

        function doUpateData(id) {
            var $div = $('<div>');
            $div.load('secUser/toUpdate.do?id=' + id, function (response, status, xhr) {
                if (xhr.status == 200) {
                    layer.open({
                        type: 1,//Page层类型
                        area: ['400px', '350px'],
                        title: '修改管理员信息',
                        btn: ['提交修改'],
                        content: $div.html(),
                        yes: function (layId) {
                            var param = $('#secUserFormModify').serialize();
                            $.post('secUser/update.do', param, function (result) {
                                if (result.success) {
                                    loadData();
                                    layer.msg('修改成功', {icon: 1});
                                    layer.close(layId)
                                } else {
                                    layer.msg('修改失败', {icon: 5});
                                }
                            }, 'json')
                        }
                    });
                }
            });
        }

    })();
</script>
